<?php

namespace common\models\member;

use common\helpers\ArrayHelper;
use common\helpers\Html;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 *
 * @property int $id
 * @property string $order_no 订单号
 * @property int $member_id 会员编号
 * @property string $amount 提现金额
 * @property string $fee 服务费
 * @property string $money 实到金额
 * @property int $created_at 提现时间
 * @property int $confirm_at 审核时间
 * @property int $type 提现类型
 * @property int $state 状态
 * @property string $open_bank 开户银行
 * @property string $bank_user_name 开户姓名
 * @property string $card_number 银行卡号
 * @property string $bank_address 开户地址
 * @property string $note 备注
 */
class MemberWithdraw extends \yii\db\ActiveRecord
{
    public $bank_id;
    public $username;
    public $start_time;
    public $end_time;
    /**
     * 删除
     */
    const STATUS_DELETED = -1;
    /**
     * 等待审核
     */
    const STATUS_WAIT = 0;
    /**
     * 已审核
     */
    const STATUS_ACTIVE = 1;
    /**
     * 审核不通过
     */
    const STATUS_NOTWAIT = 2;
    /**
     * 已发货
     */
    const STATUS_IS_SEND = 3;
    /**
     * @var array
     */
    public static $listExplain = [
        self::STATUS_DELETED => '已删除',
        self::STATUS_WAIT => '等待审核',
        self::STATUS_ACTIVE => '已审核',
        self::STATUS_NOTWAIT => '审核不通过',
        self::STATUS_IS_SEND => '已发货',
    ];

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at']
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%member_withdraw}}';
    }

  /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['member_id','amount'], 'required'],
            [['member_id', 'created_at', 'confirm_at', 'type', 'status','is_lock'], 'integer'],
            [['amount', 'fee', 'money','shuifei'], 'number'],
            [['order_no', 'open_bank', 'bank_user_name', 'card_number','mobile'], 'string', 'max' => 64],
            [['bank_address', 'note', 'address', 'pay_img'], 'string', 'max' => 255],
            [['bank_id'],'safe'],


        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'order_no' => '提现订单号',
            'member_id' => '会员编号',
            'amount' => '提现金额',
            'fee' => '服务费',
            'money' => '实付金额',
            'created_at' => '提现时间',
            'confirm_at' => '审核时间',
            'type' => '提现类型',
            'shuifei' => '税费',
            'state' => '状态',
            'open_bank' => '开户银行',
            'bank_user_name' => '收货人姓名',
            'card_number' => '卡号',
            'bank_address' => '银行地址',
            'note' => '备注',
            'bank_id'=> '银行卡',
            'is_lock'=> '锁定',
            'mobile'=> '手机号码',
            'address' => '详细地址',
            'pay_img' => '收款二维码',
        ];
    }
    public function getMember()
    {
        return $this->hasOne(Member::class,['id'=>'member_id']);
    }
    public static function getCashFee()
    {
        $fee = Yii::$app->debris->backendConfig('withdraw_fee',true);
        return $fee ?:0;
    }
  
    public static function dropList()
    {
        $arr = [ 1 => '通过',2 => '不通过'];
        return ArrayHelper::merge(['prompt'=>'选择'],$arr);
    }

    public function generateNo()
    {   
        $order_no = Date('ymd').rand(10000000,99999999);
        $hasOrder = self::find()->where(['order_no' => $order_no])->one();
        if ($hasOrder) {
          return $this->generateNo();
        }
        return $order_no;
    }

    public static function getStates()
    {
        return [
            '锁定','取消锁定','已打款','已退回'
        ];  
    }

    public static function setStates($state)
    {
        $states = static::getStates();
        return isset($states[$state]) ? $states[$state]:'未设置';
    }
    
    public static function getActionType($state)
    {
        $states = self::getStates();
        $class = ['info','success','danger'];
        return Html::tag('span',$states[$state],['class'=>'text text-'.$class[$state]]);
    }

    /**
     * 
     * @param  [type] $insert [description]
     * @return [type]         [description]
     */
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            if ($this->isNewRecord) {
                $this->order_no = $this->generateNo();
            }
            return true;
        } else {
            return false;
        }
    }
}
